package Basic.day06;

import java.util.*;


/**
 *  案例：模拟评委打分系统
 */
public class CalAvg {
    public static void main(String[] args) {
        double[] scores = inputScore(6);
        double avg = calAvg(scores);
        System.out.println("最终得分：" + avg);

    }
    /**
     * 计算最终分数
     * 规则：去掉最高分，去掉最低分，剩余分数平均分就是最终得分
     * @param scores
     * @return
     */
    private static double calAvg(double[] scores) {
        //对成绩数组排序
        Arrays.sort(scores);
        System.out.println("评委的评分从低到高为:" + Arrays.toString(scores));
        //去除最高分和最低分
        double[] newScores = new double[scores.length - 2];
        System.arraycopy(scores, 1, newScores, 0, scores.length - 2);
        System.out.println("去除最高分和最低分:" + Arrays.toString(newScores));
        //计算剩余分数平均分
        OptionalDouble average = Arrays.stream(newScores).average();
        double finalScore = average.getAsDouble();
        return finalScore;
    }

    /**
     * N个评委打分
     *
     * @param N:评委个数
     * @return
     */
    private static double[] inputScore(int N) {
        double[] scores = new double[N];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < N; i++) {
            System.out.println("请输入第" + (i + 1) + "个评委的打分：");
            scores[i] = sc.nextDouble();
        }
        return scores;
    }


}
